package com.ideal.gpt.ability.docqa;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.lzwork.utility.collection.CollUtility;
import com.lzwork.utility.file.LineContent;
import com.lzwork.utility.logger.LOGGER;
import com.lzwork.utility.str.StrUtility;

/**
 * @author LiZheng
 * @date 2023年12月15日 下午3:54:49
 */

public class QaTF
{
	static Set<String> prefix = CollUtility.arrayToSet("origin: 句子", "changed: 改写的结果", "diff: 区别");

	public QaTF(String response, boolean tf)
	{
		setResponse(response);
		this.tf = tf;

		keyOrigin.add("origin");
		keyOrigin.add("原句");
		keyOrigin.add("句子内容");

		keyChanged.add("changed");
		keyChanged.add("改写后");
		keyChanged.add("改写结果");
		keyChanged.add("改写的结果");
		keyChanged.add("改写的句子");

		keyDiff.add("diff");
		keyDiff.add("区别");

		refreshData();
	}

	public QaTF(String response, boolean tf, List<String> keyOrigin, List<String> keyChanged, List<String> diff)
	{
		setResponse(response);
		this.tf = tf;
		if (keyOrigin != null)
			this.keyOrigin.addAll(keyOrigin);
		if (keyChanged != null)
			this.keyChanged.addAll(keyChanged);
		if (keyDiff != null)
			this.keyDiff.addAll(keyDiff);

		refreshData();
	}

	String response;
	boolean tf;
	List<String> keyOrigin = new ArrayList<>();
	List<String> keyChanged = new ArrayList<>();
	List<String> keyDiff = new ArrayList<>();

	String sentence;
	String question;
	String reason;

	public String getSentence()
	{
		return sentence;
	}

	public String getQuestion()
	{
		return question;
	}

	public String getReason()
	{
		return reason;
	}

	public String getResponse()
	{
		return response;
	}

	public String toQuestion(String title, boolean noAnswer)
	{
		String result = "问题： ";

		question = question.replace("改写的结果", "");
		question = question.replace("改写的句子", "");
		question = question.replace("\n", "");

		if (StrUtility.stringValid(title))
			result += title + ", ";

		result += "以下说法是否正确：" + question;

		if (!noAnswer)
			result += "\n答案：" + (tf ? "正确" : "错误");

		return result;
	}

	void setResponse(String input)
	{
		input = input.replace("```", "");
		input = input.replace("：", ":");
		input = input.replace("\r", "");

		int len = input.length();
		input = input.replace("origin: 句子\nchanged: 改写的结果\ndiff: 区别", "");
		int len2 = input.length();

		if (len != len2)
			LOGGER.log(LOGGER.APP, "Remove useless prefix.");

		response = input;
	}

	private void refreshData()
	{
		// String input = response.replace("```", "");
		// input = input.replace("：", ":");
		// input = input.replace("\r", "");
		//
		// int len = input.length();
		// input = input.replace("origin: 句子\nchanged: 改写的结果\ndiff: 区别", "");
		// int len2 = input.length();
		//
		// if (len != len2)
		// LOGGER.log(LOGGER.APP, "Remove useless prefix.");
		String[] parts = response.split("\n");

		List<String> originHolder = new ArrayList<>();
		List<String> diffHolder = new ArrayList<>();
		List<String> changeHolder = new ArrayList<>();

		List<String> currentHolder = null;
		int state = 0;

		for (String str : parts)
		{
			if (state < 1)
			{
				String check = holderCheck(str, keyOrigin);
				if (check != null)
				{
					state = 1;
					currentHolder = originHolder;
					if (StrUtility.stringValid(check))
						currentHolder.add(check);
					continue;
				}
			}

			if (state < 2)
			{
				String check = holderCheck(str, keyChanged);
				if (check != null)
				{
					state = 2;
					currentHolder = changeHolder;
					if (StrUtility.stringValid(check))
						currentHolder.add(check);
					continue;
				}
			}

			if (state < 3)
			{
				String check = holderCheck(str, keyDiff);
				if (check != null)
				{
					state = 3;
					currentHolder = diffHolder;
					if (StrUtility.stringValid(check))
						currentHolder.add(check);
					continue;
				}
			}

			if (currentHolder != null)
			{
				String check = str.trim();
				if (StrUtility.stringValid(check))
					currentHolder.add(check);
			}
		}

		sentence = StrUtility.combineString(originHolder, "\n");
		question = StrUtility.combineString(changeHolder, "\n");
		reason = StrUtility.combineString(diffHolder, "\n");
	}

	String holderCheck(String str, List<String> keys)
	{
		for (String key : keys)
		{
			if (str.startsWith(key))
			{
				String check = str.substring(key.length());

				int index = check.indexOf(':');

				if (index >= 0)
					check = check.substring(index + 1);

				return check.trim();
			}
		}

		return null;
	}

	String cutPrefix(String sent, String key)
	{
		sent = sent.replace(key, "");
		sent = sent.trim();
		return sent;
	}

	public static void main(String[] args)
	{
		String test = LineContent.loadString(new File("v:/gpt job/tf_test.txt"));

		QaTF tf = new QaTF(test, false);

		String outString = tf.toQuestion("", false);

		System.out.println("Output: [" + outString + "]");

		if (tf.getQuestion().isEmpty())
		{
			System.out.println("Invalid changer fetch from:\n" + test);
		}
	}
}
